package com.it.you.common.utils;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TimeZone;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.joda.time.LocalDate;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;

public class DateUtils {
    public static final String SHORT_DATE_FORMAT_STR = "yyyy-MM-dd";
    public static final String LONG_DATE_FORMAT_STR = "yyyy-MM-dd HH:mm:ss";
    public static final String LONG_DATA_FORMAT_STR2 = "yyyy/MM/dd HH:mm:ss";
    public static final DateTimeFormatter SHORT_DATE_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd");
    public static final DateTimeFormatter LONG_DATE_FORMAT = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    public static final String EARLY_TIME = "00:00:00";
    public static final String LATE_TIME = "23:59:59";
    private static final TimeZone TZ = TimeZone.getTimeZone("GMT+:08:00");
    private static final DateTimeFormatter FORMAT_DATE_TIME = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter FORMAT_TIME = DateTimeFormat.forPattern("HH:mm:ss");
    private static final DateTimeFormatter FORMAT_DATE = DateTimeFormat.forPattern("yyyy-MM-dd");
    private static final DateTimeFormatter FORMAT_MONTH_DATE = DateTimeFormat.forPattern("yyyyMM");
    private static final DateTimeFormatter FORMAT_DATE_NOZONE = DateTimeFormat.forPattern("yyyy-MM-dd");
    private static final DateTimeFormatter FORMAT_DATE_TIME_NOZONE = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
    private static final DateTimeFormatter FORMAT_YYYYMMDD = DateTimeFormat.forPattern("yyyyMMdd");
    private static final DateTimeFormatter FORMAT_DATE_TIME_CHINESE = DateTimeFormat.forPattern("yyyy年MM月dd日 HH:mm");
    private static final DateTimeFormatter FORMAT_DATE_TIME_SPRIT = DateTimeFormat.forPattern("yyyy/MM/dd HH:mm:ss");
    private static final DateTimeFormatter FORMAT_DATE_OTHER_TIME = DateTimeFormat.forPattern("yyyy.MM.dd");

    public DateUtils() {
    }

    public static long string2Millis(String time) {
        return string2Millis(time, FORMAT_DATE_TIME);
    }

    public static long string2MillisNozone(String time) {
        return string2Millis(time, FORMAT_DATE_TIME_NOZONE);
    }

    public static long string2Millis(String time, DateTimeFormatter format) {
        try {
            return format.parseDateTime(time).getMillis();
        } catch (IllegalArgumentException var3) {
            var3.printStackTrace();
            return -1L;
        }
    }

    public static Date formatDateTimeNoZone(String str) {
        try {
            return FORMAT_DATE_TIME_NOZONE.parseDateTime(str).toDate();
        } catch (IllegalArgumentException var2) {
            return new Date();
        }
    }

    public static Date formatDateOtherTime(String str) {
        try {
            return FORMAT_DATE_OTHER_TIME.parseDateTime(str).toDate();
        } catch (IllegalArgumentException var2) {
            return new Date();
        }
    }

    public static Date formatDateTimeSprit(String str) {
        try {
            return FORMAT_DATE_TIME_SPRIT.parseDateTime(str).toDate();
        } catch (IllegalArgumentException var2) {
            return new Date();
        }
    }

    public static Timestamp fromPatter(String value, String patter, Timestamp defaultValue) {
        SimpleDateFormat sdf = new SimpleDateFormat(patter);

        try {
            return new Timestamp(sdf.parse(value).getTime());
        } catch (Exception var5) {
            return defaultValue;
        }
    }

    public static Calendar getCalendar() {
        return Calendar.getInstance(TZ);
    }

    public static String toDateTimeStr(Date date) {
        return (new DateTime(date)).toString(FORMAT_DATE_TIME.withZone(DateTimeZone.forTimeZone(TZ)));
    }

    public static String toTimeStr(Date date) {
        return (new DateTime(date)).toString(FORMAT_TIME.withZone(DateTimeZone.forTimeZone(TZ)));
    }

    public static String toDateStr(Date date) {
        return (new DateTime(date)).toString(FORMAT_DATE);
    }

    public static String toMonthStr(Date date) {
        return (new DateTime(date)).toString(FORMAT_MONTH_DATE.withZone(DateTimeZone.forTimeZone(TZ)));
    }

    public static Integer toyyyyMMdd(Date date) {
        return Integer.parseInt((new DateTime(date)).toString(FORMAT_YYYYMMDD.withZone(DateTimeZone.forTimeZone(TZ))));
    }

    public static Date fromDate(String v) {
        try {
            return FORMAT_DATE.parseDateTime(v).toDate();
        } catch (IllegalArgumentException var2) {
            return new Date();
        }
    }

    public static Date fromDate(Integer vs) {
        try {
            String v = Integer.toString(vs);
            Calendar calc = getCalendar();
            calc.set(1, Integer.parseInt(v.substring(0, 4)));
            calc.set(2, Integer.parseInt(v.substring(4, 6)) - 1);
            calc.set(5, Integer.parseInt(v.substring(6, 8)));
            calc.set(11, 0);
            calc.set(12, 0);
            calc.set(13, 0);
            return calc.getTime();
        } catch (Exception var3) {
            return new Date();
        }
    }

    public static int getYear(Timestamp now) {
        Calendar calc = getCalendar();
        calc.setTime(new Date(now.getTime()));
        return calc.get(1);
    }

    public static int getMonth(Timestamp now) {
        Calendar calc = getCalendar();
        calc.setTime(new Date(now.getTime()));
        return calc.get(2);
    }

    public static int getDay(Timestamp now) {
        Calendar calc = getCalendar();
        calc.setTime(new Date(now.getTime()));
        return calc.get(5);
    }

//    public static String getShortYMD(Timestamp time, int startYear) {
//        char var10000 = YvanUtil.getShortDig(getYear(time) - startYear);
//        return var10000 + YvanUtil.getShortDig(getMonth(time)) + YvanUtil.getShortDig(getDay(time));
//    }

    public static Date fromDateTime(String v) {
        try {
            return FORMAT_DATE_TIME.withZone(DateTimeZone.forTimeZone(TZ)).parseDateTime(v).toDate();
        } catch (IllegalArgumentException var2) {
            return new Date();
        }
    }

    public static Date addDay(Date v, int days) {
        Calendar calc = getCalendar();
        calc.setTime(v);
        calc.add(7, days);
        return calc.getTime();
    }

    public static List<Date> getDaySeq(Date dt1, Date dt2) {
        Calendar calc = getCalendar();
        List<Date> r = new ArrayList();
        calc.setTime(dt1);

        while (calc.getTime().compareTo(dt2) <= 0) {
            r.add(calc.getTime());
            calc.add(7, 1);
        }

        return r;
    }

    public static List<String> getMonthSeq(int d1, int d2) {
        Date dt1 = fromDate(Integer.parseInt(Integer.toString(d1) + "01"));
        Date dt2 = fromDate(Integer.parseInt(Integer.toString(d2) + "01"));
        return getMonthSeq(dt1, dt2);
    }

    public static List<String> getMonthSeq(Date dt1, Date dt2) {
        Calendar calc = Calendar.getInstance();
        calc.setTime(dt1);
        String var10000;
        if (calc.get(5) > 1) {
            var10000 = toMonthStr(dt1);
            dt1 = fromDate(Integer.parseInt(var10000.substring(0, 6) + "01"));
        }

        calc.setTime(dt2);
        if (calc.get(5) > 1) {
            var10000 = toMonthStr(dt2);
            dt2 = fromDate(Integer.parseInt(var10000.substring(0, 6) + "01"));
        }

        List<String> r = new ArrayList();
        calc.setTime(dt1);

        while (calc.getTime().compareTo(dt2) <= 0) {
            r.add(toMonthStr(calc.getTime()));
            calc.add(2, 1);
        }

        return r;
    }

    public static void main(String[] args) {
        System.out.println((new SimpleDateFormat("M.dd")).format(fromDate(20171109)));
        String d1 = "2016-02-23";
        String d2 = "2016-03-02";
        System.out.println(d1);
        System.out.println(toDateTimeStr(fromDate(20161018)));
        List<Date> seq = getDaySeq(fromDate(d1), fromDate(d2));
        Iterator var4 = seq.iterator();

        while (var4.hasNext()) {
            Date d = (Date) var4.next();
            System.out.println(toDateStr(d));
        }

    }

    public static Timestamp now() {
        return new Timestamp(System.currentTimeMillis());
    }

    public static int seconds(Timestamp t1, Timestamp t2) {
        return Conv.NI((t1.getTime() - t2.getTime()) / 1000L);
    }

    public static Timestamp addSeconds(Timestamp t1, int seconds) {
        return new Timestamp(t1.getTime() + (long) (seconds * 1000));
    }

    public static void setTime(Map map, String timeStr, String identify) {
        if (!StringUtils.isEmpty(timeStr)) {
            String[] vs = timeStr.split("至");
            if (vs.length == 2) {
                Timestamp dt1 = new Timestamp(formatDateNoZone(vs[0].trim()).getTime());
                Timestamp dt2 = new Timestamp(addDay(formatDateNoZone(vs[1].trim()), 1).getTime());
                map.put(identify + "Begin", dt1);
                map.put(identify + "End", dt2);
            }
        }

    }

    public static void setTime(Map map, String timeStr, String start, String end) {
        if (!StringUtils.isEmpty(timeStr)) {
            String[] vs = timeStr.split("至");
            if (vs.length == 2) {
                Timestamp dt1 = new Timestamp(formatDateNoZone(vs[0].trim()).getTime());
                Timestamp dt2 = new Timestamp(addDay(formatDateNoZone(vs[1].trim()), 1).getTime());
                map.put(start, dt1);
                map.put(end, dt2);
            }
        }

    }

    public static Date formatDateNoZone(String str) {
        try {
            return FORMAT_DATE_NOZONE.parseDateTime(str).toDate();
        } catch (IllegalArgumentException var2) {
            return new Date();
        }
    }

    public static String toChineseFmt(Timestamp startTime) {
        return (new DateTime(new Date(startTime.getTime()))).toString(FORMAT_DATE_TIME_CHINESE);
    }

    public static String format(Date date, String pattern) {
        return date == null ? "" : (new SimpleDateFormat(pattern)).format(date);
    }

    public static Date getEarlyInTheDay(Date date) {
        DateTime var10000 = new DateTime(date);
        String dateString = var10000.toString(SHORT_DATE_FORMAT) + " 00:00:00";

        try {
            return LONG_DATE_FORMAT.parseDateTime(dateString).toDate();
        } catch (IllegalArgumentException var3) {
            throw new RuntimeException("parser date error.", var3);
        }
    }

    public static Date getLateInTheDay(Date date) {
        DateTime var10000 = new DateTime(date);
        String dateString = var10000.toString(SHORT_DATE_FORMAT) + " 23:59:59";

        try {
            return LONG_DATE_FORMAT.parseDateTime(dateString).toDate();
        } catch (IllegalArgumentException var3) {
            throw new RuntimeException("parser date error.", var3);
        }
    }

    public static Date convertTimeStrToDate(String dateStr) {
        try {
            return LONG_DATE_FORMAT.parseDateTime(dateStr).toDate();
        } catch (IllegalArgumentException var2) {
            throw new RuntimeException("parser date error.", var2);
        }
    }

    public static Date getDate(int i) {
        Date dat = null;
        Calendar cd = Calendar.getInstance();
        cd.add(5, i);
        dat = cd.getTime();
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp date = Timestamp.valueOf(dformat.format(dat));
        return date;
    }

    public static Date getDate(int i, int calendar) {
        Date dat = null;
        Calendar cd = Calendar.getInstance();
        cd.add(calendar, i);
        dat = cd.getTime();
        SimpleDateFormat dformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp date = Timestamp.valueOf(dformat.format(dat));
        return date;
    }

    public static String timeStamp2Date(String milliseconds, String format) {
        if (milliseconds != null && !milliseconds.isEmpty() && !milliseconds.equals("null")) {
            if (format == null || format.isEmpty()) {
                format = "yyyy-MM-dd HH:mm:ss";
            }

            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.format(new Date(Long.valueOf(milliseconds)));
        } else {
            return "";
        }
    }

    public static String date2TimeStamp(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime());
        } catch (Exception var3) {
            var3.printStackTrace();
            return "";
        }
    }

    public static Long date2TimeStampLong(String date_str, String format) {
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return sdf.parse(date_str).getTime();
        } catch (Exception var3) {
            var3.printStackTrace();
            return null;
        }
    }

    public static String getSdTimeOut() {
        Calendar c = Calendar.getInstance();
        c.add(12, 180);
        return (new SimpleDateFormat("yyyyMMddHHmmss")).format(c.getTime());
    }

    public static boolean isLongDateFormatStr(String dateStr) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        formatter.setLenient(false);

        try {
            Date date = formatter.parse(dateStr);
            System.out.println(date);
            return true;
        } catch (Exception var3) {
            return false;
        }
    }

    public static long getMillsecFromStr(String dateTimeStr) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        if (dateTimeStr == null) {
            return 0L;
        } else {
            try {
                Date d = sdf.parse(dateTimeStr);
                return d.getTime();
            } catch (Exception var3) {
                return 0L;
            }
        }
    }

    public static Date today() {
        return (new LocalDate()).toDate();
    }

    public static Date sysdate() {
        return new Date();
    }

    public static Date addMonths(Date date, int months) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(2, months);
        return calendar.getTime();
    }

    public static Date addDays(Date date, int days) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(5, days);
        return calendar.getTime();
    }

    public static Date addMinutes(Date date, int minutes) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(12, minutes);
        return calendar.getTime();
    }

    public static boolean isShortDateFormatStr(String dateStr) {
        DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        formatter.setLenient(false);

        try {
            formatter.parse(dateStr);
            return true;
        } catch (Exception var3) {
            return false;
        }
    }
}

